<?php 
include('../Connections/KRAPPERCON.php');
include ("../Connections/Accesos.php");

$buscar = false;
$found = false;

$callback=getVal('callback');

$ser_veh = '';
$pla_veh = '';
$vin_veh = '';
$mot_veh = '';
$fab_veh = '';
$mod_veh = '';
$marca_veh = '';
$nomcli_veh = '';
$pmo_veh = '';
$cli_veh = '';
$obs_veh = '';
$comb_veh = '';
$oper = '';
if (isset($_GET['oper'])){
$oper = $_GET['oper'];
}
?>

<div>
  <input type="button" value="Nuevo" onClick="newVlc();">
  &nbsp;
  <input type="button" value="Editar" onClick="editVlc()">
  &nbsp;
  <input type="button" value="Grabar" onClick="saveVlc()">&nbsp;
  <input type="button" name="btn_lis_vlc" id="btn_lis_vlc" onclick="listVlc()" value="Listar Vehículos">
  &nbsp;&nbsp;
  <input type="radio" name="rb_veh" id="rb_sr_veh" value="1" checked>
  &nbsp;Por Serie&nbsp;&nbsp;<input name="rb_veh" type="radio" id="rb_sr_pla" value="2">&nbsp;Por Nro.Placa
&nbsp;&nbsp;
<input name="t_veh_bus" type="text" id="t_nr_veh" onKeyPress="introVeh(event)" size="15" placeholder='Serie de Vehículo'>
</div>

<?php 
if ($oper == 'view'){
	$buscar = true;
	
	$byOpt = $_GET['type'];
	$codVeh = $_GET['vehiculo'];
	if ($byOpt == 'S'){
	// Buscar x Serie
	}
	elseif ($byOpt == 'P'){
	// Buscar x Placa
	}
	$sql = "call sp_getVehiculoOpt('".$codVeh."','".$byOpt."');";
	$res = $mysqli->query($sql);
	doEcho($sql);
	if ($res){
		if ($row=$res->fetch_assoc()){
			$found = true;
			
			$ser_veh = $row['VehID'];
			$pla_veh = $row['VehPlaca'];
			$vin_veh = $row['VehVIN'];
			$mot_veh = $row['VehNroMot'];
			$fab_veh = $row['VehAnFab'];
			$mod_veh = $row['VehAnMod'];
			$marca_veh = $row['MarcaID'];
			$pmo_veh = $row['VehPoMot'];
			$cli_veh = $row['ClientID'];
			$obs_veh = $row['VehObs'];
			$feccre = $row['VehFeReg'];
			$userid = $row['UserID'];
			$comb_veh = $row['VehTiCom'];
			$nomcli_veh = $row['ClientNom']." ".$row['ClientApePat'].' '.$row['ClientApeMat'];
		}
	$res->close();
	$mysqli->check_result();
	}
}	


?>


<?php 
if ($found){
?>
<div class="subTit">
<?php 
echo "Veh&iacute;ulo ".$ser_veh." creado por ".$userid." el ".$feccre;
?>
</div>
<?php
}
?>
<table width="100%" id="tbl_vlc_dat" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>Nro de Placa</td>
    <td>
      <input name="t_veh_placa" title="Nro Placa" type="text" id="t_veh_placa" size="15" maxlength="10">
    </td>
    <td><strong>Serie en el Sistema :</strong></td>
    <td>
      <span name="sp_veh_serie" id="sp_veh_serie"></span>
    </td>
  </tr>
  <tr>
    <td>Nro. VIN</td>
    <td>
      <input name="t_veh_vin"  class="clsReq" title="VIN" type="text" id="t_veh_vin" size="30" maxlength="25">
    </td>
    <td>Nro Motor</td>
    <td>
      <input name="t_veh_motor" title="Nro de Motor" type="text" id="t_veh_motor" size="15" maxlength="15">
    </td>
  </tr>
  <tr>
    <td>Año Fabricación</td>
    <td>
      <input name="t_veh_fab" type="text" title="An.Fabricación" id="t_veh_fab" size="8" maxlength="4">
    </td>
    <td>Marca / Modelo</td>
    <td>
      <select name="cb_veh_marca" title="Marca" id="cb_veh_marca">
       <?php 
	  include ('../Query_Min/getmarcas.php');
	  ?>
      </select>
    </td>
  </tr>
  <tr>
    <td>Año Modelo</td>
    <td>
      <input name="t_veh_mod" type="text" id="t_veh_mod" size="8" maxlength="4">
    </td>
    <td>Cilindrada Motor:</td>
    <td>
      <input name="t_veh_pmo" class="qt" onChange="mr(this)" type="text" id="t_veh_pmo" size="10" maxlength="10">
    </td>
  </tr>
  <tr>
    <td>Combustible</td>
    <td>
      <select name="cb_veh_comb" id="cb_veh_comb">
      <option value="GA">GASOLINA</option>
      <option value="PET">DISEL</option>
      <option value="GNV">GNV</option>
      <option value="GLP">GLP</option>
      </select>
    </td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>Propietario </td>
    <td colspan="3">
      <input class="" name="t_veh_cliid" type="text" id="t_veh_cliid" size="15" maxlength="10">&nbsp;

    <input type="submit" name="btn_veh_cli" id="btn_veh_cli" value="[...]" onClick="searchCliVlc()">
    &nbsp;    <span id="sp_veh_clinom">
    </span></td>
  </tr>
  <tr>
    <td>Observación</td>
    <td colspan="3"><textarea name="t_veh_obs" cols="50" rows="3" id="t_veh_obs"></textarea></td>
  </tr>
</table>

<script type="text/javascript">

<?php 
if ($found){
?>
	GEBI('sp_veh_serie').innerHTML = '<?php echo $ser_veh;?>';
	GEBI('t_veh_placa').value = '<?php echo $pla_veh;?>';
	GEBI('t_veh_vin').value = '<?php echo $vin_veh;?>';
	GEBI('t_veh_motor').value = '<?php echo $mot_veh;?>';
	GEBI('t_veh_fab').value = '<?php echo $fab_veh;?>';
	GEBI('t_veh_mod').value = '<?php echo $mod_veh;?>';
	GEBI('cb_veh_marca').value = '<?php echo $marca_veh;?>';
	GEBI('t_veh_pmo').value = '<?php echo $pmo_veh;?>';
	GEBI('cb_veh_comb').value =  '<?php echo $comb_veh;?>';
	GEBI('t_veh_cliid').value = '<?php echo $cli_veh;?>';
	GEBI('t_veh_obs').value = '<?php echo $obs_veh;?>';
	GEBI('sp_veh_clinom').innerHTML = '<?php echo $nomcli_veh;?>';
	noEditVlc();
<?php
}
else {
	if ($buscar){
		?>
		showError('La serie/placa ingresada no existe');
		<?php 
	}
}
?>

function introVeh(e){
	if (e.keyCode == 13){
		var veh = GEBI('t_nr_veh').value;
		var opc = '';
		if (GEBI('rb_sr_veh').checked)
		opc = 'S';
		if (GEBI('rb_sr_pla').checked)
		opc = 'P';
		
		loadPage('Mantenimientos/MantVeh.php',{ oper : 'view' , vehiculo : veh , type : opc });
	}
}

function searchCliVlc(){
	loadPopup({ url : 'Consultas/findCliente.php', params : { return : 'showCliVlc' } });
}

function showCliVlc(cli){
	GEBI('t_veh_cliid').value = cli.cod;
	GEBI('sp_veh_clinom').innerHTML = cli.nom+ ' ' +cli.ape;
}

function saveVlc(){
	
	if (!$("#tbl_vlc_dat").checkInput()){
	return;
	}

	sendData('Mantenimientos/Opers/OperVeh.php',
	{
		oper : 'save',
		serie : GEBI('sp_veh_serie').innerHTML,
		placa : GEBI('t_veh_placa').value,
		vin : GEBI('t_veh_vin').value,
		nmotor : GEBI('t_veh_motor').value,
		fab : GEBI('t_veh_fab').value,
		mod : GEBI('t_veh_mod').value,
		marca : GEBI('cb_veh_marca').value,
		tmarca : $("#cb_veh_marca :selected").html(),
		pmotor : GEBI('t_veh_pmo').value,
		tcom : GEBI('cb_veh_comb').value,
		cliente : GEBI('t_veh_cliid').value,
		txt : GEBI('t_veh_obs').value,
		callback : '<?php echo $callback;?>'
	}
	,function(){});
}

function noEditVlc(){
	$("#tbl_vlc_dat select, #tbl_vlc_dat input, #tbl_vlc_dat select, #tbl_vlc_dat textarea").disable();
}

function editVlc(){
	$("#tbl_vlc_dat select, #tbl_vlc_dat input, #tbl_vlc_dat select, #tbl_vlc_dat textarea").enable();
}

function newVlc(){
	loadPage('Mantenimientos/MantVeh.php');
}

function listVlc(){
	loadPage('Consultas/ListVehiculos.php');
}
</script>